// JavaScript Document
/*====================================== 
 author:faxsun
 Copyright © 2015 faxsun. All rights reserved. 
 如需修改请注意与页面标记类名相对应
 数据处理
 author:songdragon 2015-04-29 14:21
 ======================================*/

function overlayReplyModal(id) {
	$("#replyCommentBox_" + id).overlay();
}
	
var replyComment = function(url, type, toId, commentId){//, commentToUserName){
	var commentToUserNamePH = $("#replycmt-input_" + commentId).attr('placeholder');
	var commentToUserName = commentToUserNamePH.substring(2, commentToUserNamePH.length - 1);//remove leading '回复' and trail ':'
	
	var text = $("#replycmt-input_" + commentId).val();
	$.ajax({
	    type : "POST",
		url : url, 
		data : {
			 "text" : text,
			 "type" : type,
			 "toId" : toId,
			 "commentId" : commentId,
			 "commentToUserName" : commentToUserName
	    },
	    success : function(data) {
	    	var ret = JSON.parse(data);
			if (ret.code < 0) {
				alert(ret.message);
				if (ret.code == -2) {
					// 未登录
					$('#trigger-overlay').trigger('click');
				}
			} else if (ret.code == 0) {
				if(ret.data.type == 2){
				   jumpToInbox(ret.data.authorId);
				}else{
				   loadComments();					
				}
			} else {
				alert(ret.message);
			}
		}
    });
}
	
$(function() {
	
	if ($('#reply_editor').length > 0) {
		var ue = UE.getEditor('reply_editor', {
			toolbars : [ [ 'fontfamily', // 字体
			'fontsize', // 字号
			'forecolor', // 字体颜色
			'backcolor', // 背景色
			'bold', // 加粗
			'indent', // 首行缩进
			'italic', // 斜体
			'|', 'insertorderedlist', // 有序列表
			'insertunorderedlist', // 无序列表
			'justifyleft', // 居左对齐
			'justifyright', // 居右对齐
			'justifycenter', // 居中对齐
			'justifyjustify', // 两端对齐
			'|', 'emotion' ] ],
			maximumWords : 100
		});
	}
	
	loadComments();

	$('#replycmt_form').submit(function(event) {
		var url = $('#replycmt_form').attr('action');
		event.preventDefault();
		
	});
	
	// 评论表单提交
	$('#comment_form').submit(function(event) {
		var url = $('#comment_form').attr('action');
		event.preventDefault();
		$.post(url, $('#comment_form').serialize()).done(function(data) {
			var ret = JSON.parse(data);
			if (ret.code < 0) {
				alert(ret.message);
				if (ret.code == -2) {
					// 未登录
					$('#trigger-overlay').trigger('click');
				}
			} else if (ret.code == 0) {
				// 清空编辑器
				ue.setContent('');
				if(ret.data.type == 2){
				   jumpToInbox(ret.data.authorId);
				}else{
				   loadComments();					
				}
			} else {
				alert(ret.message);
			}
		});
	});

	$('#basket_comment').on('click', function(event) {
		jumpToComment();
	});

	$('.add-comment').on('click', function(event) {
		jumpToComment();
	});

	function jumpToComment() {
		$('.art-tab-tit').find('a[s=1]').trigger('click');
	}
	
	function jumpToInbox(id) {
		var prefix = "";
		if (window.location.href.indexOf('faxsun-info-web') > -1) {
			prefix += "/faxsun-info-web";
		} else if (window.location.href.indexOf('faxsun-info') > -1) {
			prefix += "/faxsun-info";
		}
		window.location.replace(prefix + "/users/" + id);
	}
	
	function loadComments() {
		
		var prefix = "";
		if (window.location.href.indexOf('faxsun-info-web') > -1) {
			prefix += "/faxsun-info-web";
		} else if (window.location.href.indexOf('faxsun-info') > -1) {
			prefix += "/faxsun-info";
		}
		//刷新评论区
		var product_id = $('#product_id').val();
		var post_id = $('#post_id').val();
		var profile_id = $('#profile_id').val();
		if ($('#product_comment_list').length > 0) {
			$('#product_comment_list')
					.load(
							prefix + '/comment/listPage/' + product_id
									+ '?type=0&pn=1', function() {

							});
		}
		if($('#info_comment_list').length > 0){
			$('#info_comment_list').load(
					prefix + '/comment/listPage/' + post_id
							+ '?type=1&pn=1', function() {

					});
		}
		if($('#inbox_msg_list').length > 0){
			$('#inbox_msg_list').load(
					prefix + '/comment/listPage/' + profile_id
							+ '?type=2&pn=1', function() {

					});
		}
		// 刷新评论数字区域(share区块和口碑区域)，仅限产品评论
		if ($('#basket_comment').length > 0) {
			if (product_id !== '' && product_id !== undefined) {
				$.get(prefix + '/comment/list/' + product_id + '?type=0&pn=1')
						.done(
								function(data) {
									$('#basket_comment').find('span').text(
											'点评(' + data.totalCount + ')');
									$('.art-tab-tit').find('a[s=1]').text(
											'点评(' + data.totalCount + ')');
								}).fail(
								function() {
									$('#basket_comment').find('span').text(
											'点评(' + 0 + ')');
									$('.art-tab-tit').find('a[s=1]').text(
											'点评(' + 0 + ')');
								});
			}
			if (post_id !== '' && post_id !== undefined) {
				$.get(prefix + '/comment/list/' + post_id + '?type=1&pn=1')
						.done(
								function(data) {
									$('#basket_comment').find('span').text(
											'点评(' + data.totalCount + ')');
								}).fail(
								function() {
									$('#basket_comment').find('span').text(
											'点评(' + 0 + ')');
								});
			}

		}
	}
});